package com.yuexunit.mobilenurse.module.Patients.ui.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.yuexunit.mobilenurse.R;
import com.yuexunit.mobilenurse.base.AppContext;
import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.BloodCollection.ui.Act_BloodCollection_Normal;
import com.yuexunit.mobilenurse.module.Chemical.ui.Act_Chemical;
import com.yuexunit.mobilenurse.module.Login.ui.Act_Login;
import com.yuexunit.mobilenurse.module.Patients.adapter.PatientListAdapter;
import com.yuexunit.mobilenurse.module.Patients.bean.PatientList_Patinfo;
import com.yuexunit.mobilenurse.module.Patients.model.impl.PatientListModel;
import com.yuexunit.mobilenurse.module.Patients.presenter.IPatientListPresenter;
import com.yuexunit.mobilenurse.module.Patients.presenter.impl.PatientListPresenter;
import com.yuexunit.mobilenurse.module.Patients.ui.Act_Main_Normal;
import com.yuexunit.mobilenurse.module.Patients.ui.view.IPatientListView;
import com.yuexunit.mobilenurse.module.PutAdvice.ui.Act_Put;
import com.yuexunit.mobilenurse.module.SignInput.bean.All_Sqlite_SignInputBean;
import com.yuexunit.mobilenurse.module.SignInput.bean.SignsInput_Data;
import com.yuexunit.mobilenurse.widget.EmptyLayout;
import com.yuexunit.mobilenurse.widget.FooterLoadingLayout;
import com.yuexunit.mobilenurse.widget.PullToRefreshBase;
import com.yuexunit.mobilenurse.widget.PullToRefreshList;

import org.kymjs.kjframe.KJDB;
import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sslcjy on 16/1/22.
 */
public class Frag_PatientList_Normal extends KJFragment implements IPatientListView {
    @Bind(R.id.empty_layout)
    public EmptyLayout mEmptyLayout;
    @Bind(R.id.patient_list)
    public PullToRefreshList mRefreshLayout;
    @Bind(R.id.nurse_id)
    public TextView nurse_id;
    @Bind(R.id.nurse_name)
    public TextView nurse_name;
    @Bind(R.id.nurse_lesion)
    public TextView nurse_lesion;
    @Bind(R.id.onekey_upload)
    public Button onekey_upload;

    /**
     * 加载病人列表状态
     */
    public static final int DOWNLOAD_SUCCESS = 1;
    public static final int DOWNLOAD_NODATA = 0;
    public static final int DOWNLOAD_NETERROR = -1;

    private ListView mList;
    private Act_Main_Normal aty;
    //private Act_Main_Moto aty;
    private PatientListAdapter adapter;

    //病区号及病区名称
    private String Wardno, WardName;
    //护士相关信息
    private String NurseName, NurseId;

    //记录Log
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    //KJDB访问数据库
    KJDB kjdb;

    IPatientListPresenter presenter;

    ArrayList<PatientList_Patinfo> patientlist = new ArrayList<PatientList_Patinfo>();

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        aty = (Act_Main_Normal) getActivity();
        View layout;
        layout = View.inflate(aty, R.layout.frag_patientlist, null);
        ButterKnife.bind(this, layout);
        return layout;
    }

    @Override
    protected void initData() {
        super.initData();
        kjdb = KJDB.create();
        NurseId = PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID);
        NurseName = PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGNAME);
        WardName = PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_WARDNO_NAME);
        Wardno = PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_WARDNO_ID);
        listViewPreference();
        presenter = new PatientListPresenter(new PatientListModel(), this);
    }

    @Override
    protected void initWidget(View parentView) {
        super.initWidget(parentView);
        showNurseInfo(NurseId, NurseName, WardName);
        presenter.showPatientList(Wardno);
        presenter.getBaseTypes(Wardno);
        presenter.getAllTypes();
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = mRefreshLayout.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        mRefreshLayout.setPullLoadEnabled(false);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) mRefreshLayout.getFooterLoadingLayout())
                .setNoMoreDataText("已经是最新状态");

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
            }

        });

        mRefreshLayout.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                AppConfig.ISOVER_PATIENT = false;
                presenter.showPatientList(Wardno);
            }

            @Override
            public void onPullUpToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                mRefreshLayout.setHasMoreData(false);
            }
        });

        mEmptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                presenter.showPatientList(Wardno);
            }
        });
    }

    @Override
    @OnClick({R.id.out_login, R.id.onekey_upload, R.id.blood_send_btn, R.id.panic_btn, R.id.chemical_btn})
    public void onClick(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.out_login:
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.setClass(aty, Act_Login.class);
                aty.startActivity(intent);
                aty.finish();
                break;
            case R.id.onekey_upload:
                presenter.oneKeyUpload((ArrayList<All_Sqlite_SignInputBean>) kjdb.findAll(All_Sqlite_SignInputBean.class));
                break;
            case R.id.blood_send_btn:
                intent.setClass(aty, Act_BloodCollection_Normal.class);
                aty.startActivity(intent);
                break;
            case R.id.panic_btn:
                bundle.putString("source", "1");
                intent.putExtras(bundle);
                intent.setClass(aty, Act_Put.class);
                aty.startActivity(intent);
                break;
            case R.id.chemical_btn:
                bundle.putString("source", "1");
                intent.putExtras(bundle);
                intent.setClass(aty, Act_Chemical.class);
                aty.startActivity(intent);
                break;
            default:
                break;
        }
    }


    public void showNurseInfo(String NurseId, String NurseName, String WardNo) {
        nurse_id.setText(NurseId);
        nurse_name.setText(NurseName);
        nurse_lesion.setText(WardNo);
    }

    @Override
    public void oneKeyUploadXml(String ErrorMsg, ArrayList<All_Sqlite_SignInputBean> list) {
        if ("".equals(ErrorMsg)) {
            for (final All_Sqlite_SignInputBean sqlite_signInputBean : list) {
                kjdb.delete(sqlite_signInputBean);
            }
            ViewInject.toast("上传体征信息成功.");
        } else {
            ViewInject.toast(ErrorMsg);
        }
    }

    @Override
    public void showPatientList(ArrayList<PatientList_Patinfo> list) {
        AppConfig.ISOVER_PATIENT = true;
        patientlist = list;
        adapter = new PatientListAdapter(mList, patientlist, R.layout.item_patientlist);
        mList.setAdapter(adapter);
        mEmptyLayout.dismiss();
    }

    @Override
    public void PullDownRefreshState(int state) {
        switch (state) {
            case DOWNLOAD_SUCCESS:
                mRefreshLayout.onPullDownRefreshComplete();
                mRefreshLayout.onPullUpRefreshComplete();
                break;
            case DOWNLOAD_NETERROR:
                mEmptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                break;
            case DOWNLOAD_NODATA:
                mEmptyLayout.setErrorType(EmptyLayout.NODATA);
        }
    }

    @Override
    public void saveBaseTypes(ArrayList<SignsInput_Data> baseList) {
        ((AppContext) getActivity().getApplication()).setBaseTypes(baseList);
    }

    @Override
    public void saveAllTypes(ArrayList<SignsInput_Data> allList) {
        ((AppContext) getActivity().getApplication()).setAllTypes(allList);
    }

    public boolean JudgeVisitNo(final String visitno) {
        for (int i = 0; i < patientlist.size(); i++) {
            if (visitno.equals(patientlist.get(i).getVisit_no())) {
                return true;
            }
        }
        return false;
    }
}